package com.yunhe.tianhe.rentcars.service.permission.impl;

import com.yunhe.tianhe.commons.dto.Pageable;
import com.yunhe.tianhe.commons.util.StringUtils;
import com.yunhe.tianhe.rentcars.dao.permission.AccountDao;
import com.yunhe.tianhe.rentcars.entity.permission.Account;
import com.yunhe.tianhe.rentcars.service.permission.AccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author lanyangyun
 * @version V1.0
 * @Description: //TODO
 * @date 2017/10/25 0025 下午 2:26
 */
@Service
public class AccountServiceImpl implements AccountService {

    @Autowired
    private AccountDao accountDao;

    @Override
    public Account findOne(Long uId) {
        return accountDao.findOne(uId);
    }

    @Override
    public void save(Account account) {
        accountDao.save(account);
    }

    @Override
    public void delete(Long uId) {
        accountDao.delete(uId);
    }

    @Override
    public void deleteBatch(String ids) {
        if (StringUtils.notEmpty(ids)){
            String[] strings = ids.split(",");
            for (String s: strings){
                Long id = Long.valueOf(s);
                this.delete(id);
            }
        }
    }

    @Override
    public void update(Account account) {
        accountDao.update(account);
    }

    @Override
    public List<Account> findAll() {
        return accountDao.findAll();
    }

    @Override
    public Boolean verifyName(String name) {
        List<Account> userList = this.findAll();
        Boolean verify = Boolean.FALSE;
        for (int i = 0; i <userList.size(); i++){
            Account user = userList.get(i);
            if (user.getUserName().equals(name)){
                verify = Boolean.TRUE;
                break;
            }
        }
        return verify;
    }

    @Override
    public Pageable<Account> findPage(Integer offset, Integer size) {
        Pageable<Account> pageable = new Pageable();
        int count = accountDao.findCount();
        List<Account> data = null;

            data = accountDao.findList(offset,size);

        pageable.setTotal(new Long(count));
        pageable.setRows(data);
        return pageable;
    }

    @Override
    public Pageable<Account> search(Account account) {
        Pageable<Account> pageable = new Pageable();
        int count = accountDao.searchCount(account);
        List<Account> data = null;
        if (count>0){
            data = accountDao.searchList(account);
        }
        pageable.setTotal(new Long(count));
        pageable.setRows(data);
        return pageable;
    }

    //修改个人信息
    @Override
    public Long updatePerson(Account account) {
        Long code = accountDao.updatePerson(account);
        return code;
    }
}
